import random
import time
from Common.basepage import BasePage
from PageLocators.PlayerPage_locator import PlayerPageLocator as loc


class PlayerPage(BasePage):
    def click_share(self):
        if not self.judge_ele_exists(loc.play_loc):
            self.click_point(0.5, 0.5, "点击屏幕中心")
        self.click_element(loc.invite_loc, "点击开始分享")

    def click_slave_invite(self):
        self.click_element(loc.second_screen, "点击邀请后排屏幕")

    def click_ok(self):
        self.click_element(loc.ok_loc, "点击确定")

    def click_back(self):
        if not self.judge_ele_exists(loc.play_loc):
            self.click_point(0.5, 0.5, "点击屏幕中心")
        self.click_element(loc.back_loc, "点击左上角返回")

    def click_close(self):
        time.sleep(2)
        if not self.judge_ele_exists(loc.play_loc):
            self.click_point(0.5, 0.5, "点击屏幕中心")
        self.click_element(loc.close_loc, "点击结束投屏")

    # def click_previous(self):
    #     self.click_element(loc.previous_loc, "播放器页面_点击上一集")

    def click_next(self):
        if not self.judge_ele_exists(loc.play_loc):
            self.click_point(0.5, 0.5, "点击屏幕中心")
        self.click_element(loc.next_loc, "播放器页面_点击下一集")

    def click_play(self):
        if not self.judge_ele_exists(loc.play_loc):
            self.click_point(0.5, 0.5, "点击屏幕中心")
        self.click_element(loc.play_loc, "播放器页面_点击播放/暂停")

    def click_random_schedule(self):
        # {"x": 152, "y": 1400, "width": 2256, "height": 41}
        # click_x = 152 + random.randint(10, 2246)
        # click_y = 1400 + 20
        # self.click_point(click_x, click_y, "播放器页面_点击随机进度")
        if not self.judge_ele_exists(loc.play_loc):
            self.click_point(0.5, 0.5, "点击屏幕中心")
        seek_bounds = self.get_element_attribute(loc.seekbar_loc, "bounds", "播放器页面_获取进度条的坐标尺寸")
        lx, ly, rx, ry = seek_bounds['left'], seek_bounds['top'], seek_bounds['right'], seek_bounds['bottom']
        seek_center_x, seek_center_y = self.get_ele_center(loc.seekbar_loc, "播放器页面_获取进度条中心位置坐标")
        width = rx - lx
        click_x = lx + random.randint(10, width)
        click_y = seek_center_y
        self.click_point(click_x, click_y, "播放器页面_点击随机进度")

    def click_random_rate(self):
        if not self.judge_ele_exists(loc.play_loc):
            self.click_point(0.5, 0.5, "点击屏幕中心")
        self.click_element(loc.rate_loc, "点击倍速选择")
        time.sleep(1)
        rate = random.choice(
            [loc.rate_200_loc, loc.rate_150_loc, loc.rate_125_loc, loc.rate_100_loc, loc.rate_75_loc])
        self.click_element(rate, "点击选择的倍速")

    def click_random_bit(self):
        if not self.judge_ele_exists(loc.play_loc):
            self.click_point(0.5, 0.5, "点击屏幕中心")
        self.click_element(loc.bit_loc, "点击清晰度选择")
        time.sleep(1)
        bit = random.choice(
            [loc.bit_1080p_loc, loc.bit_720p_loc, loc.bit_480p_loc])
        self.click_element(bit, "点击选择清晰度")

    def open_video_by_SCHEME(self, qipuId, albumId=0, channelId=1):
        self.open_SCHEME_url(
            f"iqiyi://com.qiyi.video.iv/driver?command=play_card_video&qipuId={qipuId}&albumId={albumId}&channelId={channelId}")


if __name__ == '__main__':
    # PlayerPage("d_one").open_video_by_SCHEME(qipuId="406386200")
    while True:
        PlayerPage(device="d_one").click_random_bit()
        time.sleep(3)
    # PlayerPage(device="d_one").open_video_by_SCHEME(qipuId="406386200")
    # PlayerPage(device="d_one").click_play()
    pass
